package com.ruicar.afs.cloud.workflow.greatwall.processor.impl.objcode;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.dynamicrequest.DynamicRequest;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.vo.UserVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.AfsFlowQueryRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.CommonGetRequest;
import com.ruicar.afs.cloud.workflow.api.util.FlowMappingHelper;
import com.ruicar.afs.cloud.workflow.greatwall.dto.GwtRequest;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallWorkflowMappingInfo;
import com.ruicar.afs.cloud.workflow.greatwall.enums.GwtMsgType;
import com.ruicar.afs.cloud.workflow.greatwall.enums.GwtObjectCode;
import com.ruicar.afs.cloud.workflow.greatwall.enums.RequestPath;
import com.ruicar.afs.cloud.workflow.greatwall.processor.AbsBaseProcessor;
import com.ruicar.afs.cloud.workflow.greatwall.processor.GwfObjectCodeProcessor;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowBizDataService;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowMappingInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * The type GetApplyUserInfoProcessor
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.07.10 21:22:46
 * @since 1.0
 */
@Component
@Slf4j
public class GetApplyUserInfoProcessor extends AbsBaseProcessor implements GwfObjectCodeProcessor {
    /**
     * Afs great wall workflow mapping info service
     * <p>Description: </p>
     */
    private final AfsGreatWallWorkflowMappingInfoService afsGreatWallWorkflowMappingInfoService;
    /**
     * Dynamic request
     * <p>Description: </p>
     */
    private final DynamicRequest dynamicRequest;

    /**
     * Get apply user info processor
     *
     * @param afsGreatWallWorkflowBizDataService     afs great wall workflow biz data service
     * @param afsGreatWallWorkflowMappingInfoService afs great wall workflow mapping info service
     * @param dynamicRequest                         dynamic request
     */
    public GetApplyUserInfoProcessor(AfsGreatWallWorkflowBizDataService afsGreatWallWorkflowBizDataService, AfsGreatWallWorkflowMappingInfoService afsGreatWallWorkflowMappingInfoService, DynamicRequest dynamicRequest) {
        super(afsGreatWallWorkflowBizDataService);
        this.afsGreatWallWorkflowMappingInfoService = afsGreatWallWorkflowMappingInfoService;
        this.dynamicRequest = dynamicRequest;
    }

    /**
     * Belong msg gwt msg type
     *
     * @return the gwt msg type
     */
    @Override
    public GwtMsgType belongMsg() {
        return GwtMsgType.GET_PROCESSOR_INFO;
    }

    /**
     * Object code gwt object code
     *
     * @return the gwt object code
     */
    @Override
    public GwtObjectCode objectCode() {
        return GwtObjectCode.GET_APPLY_USER;
    }

    /**
     * Process string
     *
     * @param request request
     * @return the string
     */
    @Override
    public String process(GwtRequest request) {
        log.info("获取指定处理人信息===>获取经销商信息===>{}", request);
        checkException(request.getServiceId(), request.getDataId());
        JSONObject rtnObject = new JSONObject();
        rtnObject.put("{@ret}", new JSONArray());
        AfsGreatWallWorkflowMappingInfo afsGreatWallWorkflowMappingInfo = afsGreatWallWorkflowMappingInfoService.queryMappingByServiceId(request.getServiceId());
        try {
            if (afsGreatWallWorkflowMappingInfo != null) {
                AfsFlowQueryRequest<CommonGetRequest> afsFlowQueryRequest = new AfsFlowQueryRequest<>();
                afsFlowQueryRequest.setAfsFlowKey(afsGreatWallWorkflowMappingInfo.getAfsFlowKey());
                CommonGetRequest commonGetRequest = new CommonGetRequest();
                commonGetRequest.setServiceId(request.getServiceId());
                commonGetRequest.setBizDataId(request.getDataId());
                commonGetRequest.setAfsFlowKey(FlowMappingHelper.getAfsKey(request.getServiceId()));
                commonGetRequest.setBodyData(request.getBody());
                afsFlowQueryRequest.setData(commonGetRequest);
                IResponse<UserVo> response =
                        dynamicRequest.post(afsGreatWallWorkflowMappingInfo.getServiceName(), AfsEnumUtil.key(RequestPath.GET_APPLY_USER_INFO), afsFlowQueryRequest, new TypeReference<UserVo>() {
                        });
                Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(response.getCode()), "feign 获取经销商信息信息失败");
                if (response.getData() != null && StringUtils.isNotEmpty(response.getData().getUsername())) {
                    JSONObject temp = new JSONObject();
                    temp.put("ID", response.getData().getUsername());
                    rtnObject.getJSONArray("{@ret}").add(temp);
                }
            }
        } catch (Exception e) {
            log.error("获取经销商信息失败", e);
        }
        return rtnObject.toJSONString();
    }


}
